
<template>
  <div :id="id" :class="className" style="height:100%;width:100%" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
// 饼装图封装
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    title: {
      type: String,
      default: 'chart'
    },
    color: {
      type: Array,
      default() {
        return []
      }
    },
    series: {
      type: Array,
      default() {
        return []
      }
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    series(val) {
      // this.setOptions(val)
      this.series = val
      this.initChart(val)
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: this.title,
          subtext: this.subtext,
          bottom: '0%',
          left: 'center',
          textStyle: {
            fontSize: 14,
            color: '#333333'
          }
        },
        color: this.color,
        tooltip: {
          trigger: 'item',
          formatter: '{b} : {c} ({d}%)'
        },
        series: this.series
      })
    }
  }
}
</script>
